import Request from "@/sdk/pocky-request/index.js";
import TL from "../../common/tool.js";
import Vue from 'vue';
var vm = new Vue();
const user = {
	state: {
		userInfo: {}, //用户资料
		isLogin: false, //登录状态
		token: null //token
	},
	mutations: {
		// 更新用户资料
		updateUserInfo(state, _userInfo) {
			state.userInfo = _userInfo
		},
		// 切换登录状态
		toggleIsLogin(state, isLogin) {
			state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
		},
		// 设置token
		setToken(state, _token) {
			uni.setStorageSync('token', _token);
		},
		// 重置
		resetUser(state) {
			Object.assign(state, {
				userInfo: {},
				isLogin: false,
				token: null
			});
			uni.removeStorageSync("token")
		}
	},
	getters: {
		// '../../static/userPic.png'
		getUserInfo: (state, getters) => {
			return state.userInfo;
		}
	},
	actions: {
		// 用户注册
		register(context, _data) {
			let r = Request();
			r.request({
				url: '/common/regist.do',
				method: 'post',
				data: {
					username: _data.username,
					password: _data.password,
					code: _data.code
				}
			}).then(res => {
				if (res.data.code == 1) {
					uni.showToast({
						title: '注册成功',
						icon: 'none'
					});
					context.commit('updateUserInfo', res.data.data)
					context.commit('setToken', res.data.data.token)
					context.commit('toggleIsLogin')
					setTimeout(() => {
						uni.reLaunch({
							//跳转到创建店铺的页面
							url: '../addShop/addShop'
						})
					}, 500)
				} else {
					uni.showToast({
						title: '注册失败,' + res.data.msg,
						icon: 'none'
					});
				}
			})
		},
		// 通过用户名和密码登录
		loginByName(context, _data) {
			let r = Request();
			r.request({
					url: '/common/login.do',
					method: 'post',
					data: {
						username: _data.username,
						password: _data.password
					}
				})
				.then(res => {
					if (res.data.code == 1) {
						context.commit('updateUserInfo', res.data.data)
						context.commit('setToken', res.data.data.token)
						context.commit('toggleIsLogin')
						uni.reLaunch({
							url: '/pages/selectShop/selectShop'
						})
						// if (res.data.data.admin == 1) {
						// 	vm.$store.dispatch("getSettledByUid", res.data.data.userId).then(settledRes => {
						// 		if (settledRes.data.data.status == 0) { //未提交审核
						// 			uni.reLaunch({
						// 				url: '/pages/addSettled/addSettled'
						// 			})
						// 		} else { //审核通过
						// 			uni.reLaunch({
						// 				url: '/pages/selectShop/selectShop'
						// 			})

						// 		}
						// 	})
						// } else {
						// 	uni.reLaunch({
						// 		url: '/pages/selectShop/selectShop'
						// 	})
						// }

					} else {
						uni.showToast({
							title: "登录失败" + res.data.msg,
							icon: 'none'
						});
					}
				})
		},
		// 通过短信验证码登录
		loginByMsgCode(context, _data) {
			let r = Request();
			r.request({
					url: '/common/login.do',
					method: 'post',
					data: {
						username: _data.username,
						code: _data.code
					}
				})
				.then(res => {
					console.log(res)
					if (res.data.code == 1) {
						context.commit('updateUserInfo', res.data.data)
						context.commit('setToken', res.data.data.token)
						context.commit('toggleIsLogin')
						// vm.$store.dispatch("getSettledByUid", res.data.data.userId).then(settledRes => {
						// 	if (settledRes.data.data.status == 0) { //未提交审核
						// 		uni.reLaunch({
						// 			url: '/pages/addSettled/addSettled'
						// 		})
						// 	} else { //审核通过
						// 		uni.reLaunch({
						// 			url: '/pages/selectShop/selectShop'
						// 		})

						// 	}
						// })
						uni.reLaunch({
								url: '/pages/selectShop/selectShop'
						 		})
					} else {
						uni.showToast({
							title: "登录失败" + res.data.msg,
							icon: 'none'
						});
					}
				})
		},
		// 通过token登录
		loginByToken(context) {
			return new Promise((resolve, reject) => {
				if (uni.getStorageSync("token")) {
					Request().request({
						url: '/common/login/auto.do',
						method: 'post',
						data: {
							token: uni.getStorageSync("token")
						}
					}).then(res => {
						if (res.data.code == 1) {
							context.commit('updateUserInfo', res.data.data)
							context.commit('setToken', res.data.data.token)
							context.commit('toggleIsLogin')
							resolve(res)
						} else {
							uni.showToast({
								title: "您的登录信息已过期,请重新登录",
								icon: 'none'
							});
							reject(res)
						}
					});
				} else {
					reject()

				}
			})

		},
		// 获取短信验证码
		sendMsgCode(context, _data) {
			return new Promise(resolve => {
				let r = Request();
				r.request({
						url: '/common/send_sms.do',
						method: 'post',
						data: _data
					})
					.then(res => {
						if (res.data.code == 1) {
							uni.showToast({
								title: '验证码发送成功,请注意查收',
								icon: 'none'
							});
							resolve(res);
						} else {
							uni.showToast({
								title: "发送失败," + res.data.msg,
								icon: 'none'
							});
							resolve(res);
						}
					})
			})

		},
		// 通过短信验证码设置新密码
		setNewPass(context, _data) {
			let r = Request();
			r.request({
				url: '/user/modify_password.do',
				method: 'post',
				data: {
					username: _data.username,
					password: _data.password,
					code: _data.code
				}
			}).then(res => {
				if (res.data.code == 1) {
					uni.showToast({
						title: '密码修改成功，请重新登录',
						icon: 'none'
					});
					context.commit('resetUser')
					setTimeout(() => {
						uni.reLaunch({
							url: '../loging/loging'
						});
					}, 1000)
				} else {
					uni.showToast({
						title: "修改失败," + res.data.msg,
						icon: 'none'
					});
				}
			})
		},
		// 修改用户信息
		editUserInfo(context, _data) {
			let r = Request();
			r.request({
				url: '/user/update_info.do',
				method: 'post',
				data: _data
			}).then(res => {
				if (res.data.code == 1) {
					// uni.showToast({
					// 	title: '修改成功',
					// 	icon: 'none'
					// });
					uni.hideLoading()
				} else {
					uni.showToast({
						title: "修改失败," + res.data.msg,
						icon: 'none'
					});
				}
			})
		},
		// 验证旧手机号
		checkOldTel(context, _data) {
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/verify_phone.do',
					method: 'post',
					data: {
						phone: _data.phone,
						code: _data.code
					}
				}).then(res => {
					if (res.data.code == 1) {
						resolve();
					} else {
						uni.showToast({
							title: "验证失败," + res.data.msg,
							icon: 'none'
						});
					}
				})
			})

		},
		// 绑定新手机号
		bindNewTel(context, _data) {
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/update_phone.do',
					method: 'post',
					data: {
						userId: _data.userId,
						phone: _data.phone,
						code: _data.code
					}
				}).then(res => {
					if (res.data.code == 1) {
						uni.showToast({
							title: "绑定新手机号成功",
							icon: 'none'
						});
						resolve();
					} else {
						uni.showToast({
							title: "绑定失败," + res.data.msg,
							icon: 'none'
						});
					}
				})
			})

		},
		// 退出
		logout(context) {
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/common/logout.do',
					method: 'post',
					data: {
						token: uni.getStorageSync("token")
					}
				}).then(res => {
					if (res.data.code == 1) {
						context.commit('toggleIsLogin')
						context.commit('resetUser')
						setTimeout(() => {
							uni.reLaunch({
								url: '../login/login'
							});
						}, 500)

					} else {
						uni.showToast({
							title: "退出失败," + res.data.msg,
							icon: 'none'
						});
					}
				})
			})
		},
		getTodayClassList(context, _data) {
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/checkin/timeable/list.do',
					method: 'post',
					data: _data
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						if (res.data.data.length) {
							let _weekFmt = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
							let _dateStr = TL.datePattern(new Date(), 'yyyy-MM-dd');
							let _week = new Date().getDay() == 0 ? 6 : (new Date().getDay() - 1);
							let _arr = res.data.data.sort((a, b) => {
								return TL.compareTimeStr(a.begin, b.begin);
							})
							_arr = TL.classTableGroup(_arr, 'classDate');
							_arr = _arr.find((x, i) => {
								return x.group == _weekFmt[_week]
							});
							let _ar2 = _arr.data.filter((x, i) => {
								return ((x.weekNum - 1) == _week) && (TL.dateStrToNum(_dateStr) >= TL.dateStrToNum(x.startDate)) && (!x.endTime ||
									(x.endTime && TL.dateStrToNum(_dateStr) <= TL.dateStrToNum(x.endTime)))
							})
							if (_ar2) {
								resolve(_ar2);
							} else {
								resolve([]);
							}

						} else {
							resolve([]);
						}
					} else {
						resolve([]);
						uni.showToast({
							title: '获取今日课表失败',
							icon: 'none'
						});
					}
				})
			})
		},
		/**获取门店课表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetStoreTable(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/checkin/timeable/store.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						// uni.showToast({
						// 	title: "门店课表获取失败," + res.data.msg,
						// 	icon: 'none'
						// });
					}
				})
			})
		}
	}
}

export default user
